﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace LithuanianOutLoud
{

    public class LOOMediaItem
    {
        public string title;
        public string description;
        public string mediaUrl;
    }


    public class RSSFeedParser
    {
        public List<LOOMediaItem> LOOMediaItems = new List<LOOMediaItem>();


        public void ParseRssFeed(string url)
        {
            using(XmlReader reader = XmlReader.Create(url))
            {
                reader.MoveToContent();
                string title = null;
                string description = null;
                string mediaUrl = null;

                while (reader.Read())
                {
                    if (reader.NodeType == XmlNodeType.Element)
                    {
                        if (reader.Name == "item")
                        {
                            // new item, see if we should flush data            
                            if (mediaUrl != null)
                            {
                                LOOMediaItem tmp = new LOOMediaItem();
                                tmp.title = title;
                                tmp.description = description;
                                tmp.mediaUrl = mediaUrl;
                                LOOMediaItems.Add(tmp);
                            }
                            // Reset
                            mediaUrl = null;
                            description = null;
                            title = null;
                        }
                        else if (reader.Name == "title")
                        {
                            title = reader.ReadElementContentAsString();
                        }
                        else if (reader.Name == "description")
                        {
                            description = reader.ReadElementContentAsString();
                        }
                        else if (reader.Name == "enclosure")
                        {
                            string tmp = reader.GetAttribute("url");
                            if (tmp.ToLowerInvariant().EndsWith("mp3"))
                            {
                                mediaUrl = reader.GetAttribute("url");
                            }
                        }
                    }
                }
            
                //Last item that needs flushing?         
                if (mediaUrl != null)
                {
                    LOOMediaItem tmp = new LOOMediaItem();
                    tmp.title = title;
                    tmp.description = description;
                    tmp.mediaUrl = mediaUrl;
                    LOOMediaItems.Add(tmp);
                }
            }

            // Reverse sort list so that first item is the oldest entry in the podcast series
            LOOMediaItems.Reverse();
        }
    }
}
